package com.rules.admin.vo;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.rules.admin.domain.IRole;
import com.rules.admin.domain.ITable;
import com.rules.admin.domain.IUser;
import com.rules.admin.entity.SysUser;

import java.io.Serializable;
import java.util.*;

public class RuleUser extends SysUser implements IUser, Serializable {
   public static final RuleUser ANONYMOUS_USER = new RuleUser();
   private List<Long> groupIds;
   private Set<String> hasPermissionPatterns = Sets.newConcurrentHashSet();
   private Set<IRole> authorities = Sets.newConcurrentHashSet();
   public Map<Long, ProjectRoleVO> projectRoleVOs = Maps.newConcurrentMap();

   public String getUserId() {
      return String.valueOf(this.getId());
   }

   public Collection<IRole> getAuthorities() {
      return this.authorities;
   }

   public String getPassword() {
      return super.getPassword();
   }

   public String getUsername() {
      return super.getUsername();
   }

   public String getCreaterId() {
      return super.getCreaterId();
   }

   public Date getCreateTime() {
      return super.getCreateTime();
   }

   public String getUpdaterId() {
      return super.getUpdaterId();
   }

   public String getEmail() {
      return super.getEmail();
   }

   public boolean isAccountNonExpired() {
      return false;
   }

   public boolean isAccountNonLocked() {
      return false;
   }

   public boolean isCredentialsNonExpired() {
      return false;
   }

   public boolean isEnabled() {
      return false;
   }

   public void setAuthorities(Iterable<? extends IRole> iterable) {
      this.authorities = Sets.newConcurrentHashSet(iterable);
   }

   public Set<String> getHasPermissionUrlPatterns() {
      return this.hasPermissionPatterns;
   }

   public void setHasPermissionPatterns(Set<String> hasPermissionPatterns) {
      this.hasPermissionPatterns = hasPermissionPatterns;
   }

   public Collection<ITable> getPermissionTables() {
      return Lists.newArrayList();
   }

//  public List<Long> getGroupIds() {
//      UserGroupMapper userGroupMapper = (UserGroupMapper)ContextHolder.getBean(UserGroupMapper.class);
//      UserGroupExample userGroupExample = new UserGroupExample();
//      userGroupExample.createCriteria().andUserIdEqualTo(this.getId());
//      return Lists.transform(userGroupMapper.selectByExample(userGroupExample), new Function<UserGroup, Long>() {
//         public Long apply(UserGroup input) {
//            return input.getGroupId();
//         }
//      });
//   }

   public void setGroupIds(List<Long> groupIds) {
      this.groupIds = groupIds;
   }

//   public Map<Long, ProjectRoleVO> getProjectRoleVOs() {
//      return ((ProjectService)ContextHolder.getBean(ProjectService.class)).getProjectRoleVOs(this.getId());
//   }

//   public void setProjectRoleVOs(Map<Long, ProjectRoleVO> projectRoleVOs) {
//      this.projectRoleVOs = projectRoleVOs;
//   }

   static {
      ANONYMOUS_USER.setUsername("匿名用户");
      ANONYMOUS_USER.setId(-1L);
   }
}
